import requests
import time
import hmac
import hashlib
import urllib.parse

"""

API密钥
oMlYVBgANplqH7XilK0rzJ47r7p1R3MWoywVNBYOGvj5UHYXebQnxZjX9kLpdd5V
密钥:
8587vRjV45QsPPXhlrn3vcBdUwQJFA6HIBqfaVbJO7UqhhqTcVrQCR4GEO6w4OtN
"""


# 您的API信息
api_key = 'oMlYVBgANplqH7XilK0rzJ47r7p1R3MWoywVNBYOGvj5UHYXebQnxZjX9kLpdd5V'
secret_key = '8587vRjV45QsPPXhlrn3vcBdUwQJFA6HIBqfaVbJO7UqhhqTcVrQCR4GEO6w4OtN'

# 基础URL
base_url = 'https://api.binance.com'

# 构建请求参数
params = {
    'symbol': 'BTCUSDT',
    'timestamp': int(time.time() * 1000),  # 当前时间戳（毫秒）
    'recvWindow': 5000  # 可选，接收窗口
    # 'startTime': 1633046400000,  # 可选，指定开始时间
    # 'endTime': 1633132800000,    # 可选，指定结束时间
    # 'limit': 500                 # 可选，限制返回数量
}

# 对参数进行签名
query_string = urllib.parse.urlencode(params)
signature = hmac.new(secret_key.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
params['signature'] = signature

# 设置请求头
headers = {
    'X-MBX-APIKEY': api_key
}

# 发送GET请求
response = requests.get(f"{base_url}/api/v3/myTrades", params=params, headers=headers)

# 打印结果
if response.status_code == 200:
    trades = response.json()
    for trade in trades:
        print(f"Time: {trade['time']}, Symbol: {trade['symbol']}, Price: {trade['price']}, Qty: {trade['qty']}")
else:
    print("Error:", response.status_code, response.text)